/**
  * @File    :   main.c
  * @Time    :   2020/04/21 22:16:10
  * @Author  :   huenrong
  */

#include <stdio.h>

#include "./opt3001/opt3001.h"
#include "./veml6075/veml6075.h"
#include "./ap3216c/ap3216c.h"
#include "./sht3x/sht3x.h"
#include "./bh1750fvi/bh1750fvi.h"

#define I2C_DEV_PATH "/dev/i2c-0"

int main(int argc, char *argv[])
{
    int fd = -1;
    int ret = -1;
    uint16_t read_ir = 0;   // 读取到的ir值
    uint16_t read_als = 0;  // 读取到的als值
    uint16_t read_ps = 0;   // 读取到的ps值
    uint32_t read_lux = 0;  // 读取到的光照强度值
    uint16_t read_uva = 0;  // 读取到的紫外线强度值
    float read_temp = 0.0;  // 读取到的温度值
    float read_hum = 0.0;   // 读取到的湿度值
    float read_light = 0.0; // bh1750fvi读取到的光照强度值

    float calc_lux = 0.0; // 计算后的光照强度值
    float calc_uva = 0.0; // 计算后的紫外线强度值

    // 打开I2C总线文件
    fd = open(I2C_DEV_PATH, O_RDWR);
    if (fd < 0)
    {
        perror("open dev fail");

        return -1;
    }

    // 初始化ap3216c
    ret = ap3216c_init(fd);
    if (0 != ret)
    {
        printf("ap3216c init fail\n");
        close(fd);

        return -1;
    }
#if 0
    // 初始化opt3001
    ret = opt3001_init(fd);
    if (0 != ret)
    {
        printf("opt3001 init fail\n");
        close(fd);

        return -1;
    }

    // 初始化veml6075
    ret = veml6075_init(fd);
    if (0 != ret)
    {
        printf("veml6075 init fail\n");
        close(fd);

        return -1;
    }
#endif
    // 循环读取各传感器数据
    while (1)
    {
        read_ir = 0;
        read_als = 0;
        read_ps = 0;
        ret = ap3216c_read_data(&read_ir, &read_als, &read_ps, fd);
        // 读取失败
        if (0 != ret)
        {
            printf("ap3216c read data fail\n");
            close(fd);

            return -1;
        }
#if 0
        // 读取光照强度
        read_lux = 0;
        ret = opt3001_read_lux(&read_lux, fd);
        // 读取失败
        if (0 != ret)
        {
            printf("opt3001 read lux fail\n");
            close(fd);

            return -1;
        }

        // 触发紫外线
        ret = veml6075_trig(fd);
        if (0 != ret)
        {
            printf("veml6075 trig fail\n");
            close(fd);

            return -1;
        }

        // 读取紫外线强度
        read_uva = 0;
        ret = veml6075_read_uva(&read_uva, fd);
        if (0 != ret)
        {
            printf("veml6075 read uva fail\n");
            close(fd);

            return -1;
        }

        // 读取温湿度
        read_temp = 0.0;
        read_hum = 0.0;
        ret = sht3x_read_temp_hum(&read_temp, &read_hum, fd);
        // 读取失败
        if (0 != ret)
        {
            printf("sht3x read data fail\n");
            close(fd);

            return -1;
        }
#endif
        // 读取光照强度
        ret = bh1750fvi_read_light(&read_light, fd);
        // 读取失败
        if (0 != ret)
        {
            printf("bh1750fvi read light fail\n");
            close(fd);

            return -1;
        }

        calc_lux = (read_lux / 100.0);
        calc_uva = (read_uva / 100.0);
        printf("current ir: %d, current als: %d, current ps: %d\n",
               read_ir, read_als, read_ps);
        printf("current lux: %.2f (lux)\n", calc_lux);
        printf("current uva: %.2f (uW/cm2)\n", calc_uva);
        printf("current temp: %.2f ('C)\n", read_temp);
        printf("current hum: %.2f (%%RH)\n", read_hum);
        printf("current light: %.2f (lux)\n\n", read_light);

        sleep(1);
    }

    close(fd);

    return 0;
}
